//
//  SurnameOfPicCell.m
//  projectOfTeamSample
//
//  Created by lk on 15/11/18.
//  Copyright © 2015年 liukai. All rights reserved.
//

#import "SurnameOfPicCell.h"
#import "UIImageView+WebCache.h"

@interface SurnameOfPicCell ()

@property (nonatomic, strong) UIImageView *headerImage;  // 头像
@property (nonatomic, strong) UILabel *nameLabel;     // 作者
@property (nonatomic, strong) UILabel *textsLabel;    // 内容
@property (nonatomic, strong) UIImageView *picImage;  // 笑图

@property (nonatomic, strong) UILabel *diggLabel;     // 点赞数
@property (nonatomic, strong) UILabel *commentLabel;  // 评论数
@property (nonatomic, strong) UILabel *shareLabel;    // 分享数

@property (nonatomic, strong) UIButton *zanButton;    // 点按钮
@property (nonatomic, strong) UIButton *pingButtoon;  // 评按钮
@property (nonatomic, strong) UIButton *fenButton;    // 分按钮

@property (nonatomic, assign) BOOL isDo; // button点击状态

@end

@implementation SurnameOfPicCell

// 初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        [self setupSubViews];
    }
    
    return self;
}

- (void)setupSubViews
{
    // 实例化
    self.textsLabel = [[UILabel alloc] init];
    self.headerImage = [[UIImageView alloc] init];
    self.nameLabel = [[UILabel alloc] init];
    self.picImage = [[UIImageView alloc] init];
    self.diggLabel = [[UILabel alloc] init];
    self.commentLabel = [[UILabel alloc] init];
    self.shareLabel = [[UILabel alloc] init];
    self.zanButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.pingButtoon = [UIButton buttonWithType:UIButtonTypeSystem];
    self.fenButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    // 设置行数
    self.textsLabel.numberOfLines = 0;
    
    // 设置font
    self.textsLabel.font = [UIFont systemFontOfSize:18];
    self.diggLabel.font = [UIFont systemFontOfSize:13];
    self.commentLabel.font = [UIFont systemFontOfSize:13];
    self.shareLabel.font = [UIFont systemFontOfSize:13];
    
    // 添加到父视图
    [self.contentView addSubview:self.textsLabel];
    [self.contentView addSubview:self.headerImage];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.picImage];
    [self.contentView addSubview:self.diggLabel];
    [self.contentView addSubview:self.commentLabel];
    [self.contentView addSubview:self.shareLabel];
    [self.contentView addSubview:self.zanButton];
    [self.contentView addSubview:self.pingButtoon];
    [self.contentView addSubview:self.fenButton];
    
    // Button添加事件
    [self.zanButton addTarget:self action:@selector(textbutton) forControlEvents:UIControlEventTouchUpInside];
    [self.pingButtoon addTarget:self action:@selector(textbutton) forControlEvents:UIControlEventTouchUpInside];
    [self.fenButton addTarget:self action:@selector(textbutton) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark - button 点击测试

- (void)textbutton
{
    
    
    if (!self.isDo)
    {
        self.isDo = !self.isDo;
        
        // 给三个button改变背景图片
        [self.zanButton setBackgroundImage:[UIImage imageNamed:@"changeZan"] forState:UIControlStateNormal];
        [self.pingButtoon setBackgroundImage:[UIImage imageNamed:@"ping"] forState:UIControlStateNormal];
        [self.fenButton setBackgroundImage:[UIImage imageNamed:@"fen"] forState:UIControlStateNormal];
        
        NSLog(@"点赞");
    }
    
    else
    {
        self.isDo = !self.isDo;
        
        // 给三个button改变背景图片
        [self.zanButton setBackgroundImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
        [self.pingButtoon setBackgroundImage:[UIImage imageNamed:@"ping"] forState:UIControlStateNormal];
        [self.fenButton setBackgroundImage:[UIImage imageNamed:@"fen"] forState:UIControlStateNormal];
        
        NSLog(@"取消点赞");
    }
    
    
}

#pragma mark - 重写setter方法
- (void)setSurnameOfPicInfo:(SurnameOfPicInfo *)surnameOfPicInfo
{
    if (_surnameOfPicInfo != surnameOfPicInfo)
    {
        _surnameOfPicInfo = surnameOfPicInfo;
        
        [self layoutModel];
    }
}

- (void)layoutModel
{
    // 获取设备的frame
    CGRect frame = [UIScreen mainScreen].bounds;
    
    // 获取textsLabel的高度
    CGFloat titleHeight = [[self class] text:self.surnameOfPicInfo.text width:frame.size.width - 20 font:18];
    
    // 设置控件的frame
    self.headerImage.frame = CGRectMake(10, 10, 45, 45);
    self.nameLabel.frame = CGRectMake(65, 18, 100, 25);
    self.textsLabel.frame = CGRectMake(10, 65, frame.size.width - 20, titleHeight);
    self.picImage.frame = CGRectMake(10, 75 + titleHeight, frame.size.width - 20, self.surnameOfPicInfo.scale * (frame.size.width - 20));
    
    self.diggLabel.frame = CGRectMake(10, 85 + titleHeight + self.picImage.frame.size.height, 50, 20);
    self.commentLabel.frame = CGRectMake(80, 85 + titleHeight + self.picImage.frame.size.height, 50, 20);
    self.shareLabel.frame = CGRectMake(150, 85 + titleHeight + self.picImage.frame.size.height, 50, 20);
    
    self.zanButton.frame = CGRectMake(10, 115 + titleHeight + self.picImage.frame.size.height, 20, 20);
    self.pingButtoon.frame = CGRectMake(80, 115 + titleHeight + self.picImage.frame.size.height, 23, 23);
    self.fenButton.frame = CGRectMake(150, 115 + titleHeight + self.picImage.frame.size.height, 20, 20);
    
    // 设置属性picImage的高度
    self.picImageHeight = self.picImage.frame.size.height;
    
    // 给三个button设置背景图片
    [self.zanButton setBackgroundImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
    [self.pingButtoon setBackgroundImage:[UIImage imageNamed:@"ping"] forState:UIControlStateNormal];
    [self.fenButton setBackgroundImage:[UIImage imageNamed:@"fen"] forState:UIControlStateNormal];
    
    
    // 赋值
    self.textsLabel.text = self.surnameOfPicInfo.text;
    self.nameLabel.text = self.surnameOfPicInfo.name;
    
    self.diggLabel.text = [NSString stringWithFormat:@"%@", self.surnameOfPicInfo.digg_count];
    self.commentLabel.text = [NSString stringWithFormat:@"%@", self.surnameOfPicInfo.comment_count];
    self.shareLabel.text = [NSString stringWithFormat:@"%@", self.surnameOfPicInfo.share_count];
    
    
    // 图片赋值
    [self.headerImage sd_setImageWithURL:[NSURL URLWithString:self.surnameOfPicInfo.avatar_url]];
    [self.picImage sd_setImageWithURL:[NSURL URLWithString:self.surnameOfPicInfo.picurl] placeholderImage:[UIImage imageNamed:@"placeholderImage.jpg"]];
    
    // 将图片设置为原型
    self.headerImage.layer.cornerRadius = 22.5;
    
    // 超过边界的不显示
    self.headerImage.layer.masksToBounds = YES;
}


#pragma mark - 自适应高度

+ (CGFloat)CellOfHeightWithSurnameInfo:(SurnameOfPicInfo *)surnameOfPicInfo
{
    CGRect frame = [UIScreen mainScreen].bounds;
    
    CGFloat titleHeight = [self text:surnameOfPicInfo.text width:frame.size.width - 20 font:18];
    
    return 150 + titleHeight;
}


+ (CGFloat)text:(NSString *)text width:(CGFloat)width font:(CGFloat)font
{
    CGSize size = CGSizeMake(width, 1000);
    
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    
    return frame.size.height;
}









@end

